package com.vimaltuts.mycontacts;

import java.io.ByteArrayOutputStream;

import com.vimaltuts.mycontacts.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class AddEditContacts extends Activity {

	private EditText name;
	private EditText phone;
	private EditText mail;
	private EditText fb;
	private Button upload;
	private Bitmap yourSelectedImage;
	private ImageView photo;
	private Button save;
	private int id;
	private byte[] blob=null;
	byte[] image=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_contact);
		setUpViews();
	}

	private void setUpViews() {

		name = (EditText) findViewById(R.id.add_name);
		phone = (EditText) findViewById(R.id.add_phone);
		mail = (EditText) findViewById(R.id.add_email);
		fb = (EditText) findViewById(R.id.add_fb);
		photo = (ImageView) findViewById(R.id.photo);	
		
		Bundle extras = getIntent().getExtras();

		if (extras != null) {
			id=extras.getInt("id");
			name.setText(extras.getString("name"));
			phone.setText(extras.getString("phone"));
			mail.setText(extras.getString("email"));
			fb.setText(extras.getString("fb"));
			image = extras.getByteArray("blob");

			if (image != null) {
				if (image.length > 3) {
					photo.setImageBitmap(BitmapFactory.decodeByteArray(image,0,image.length));
				}
			}
			
		}


		upload = (Button) findViewById(R.id.upload);
		upload.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.setType("image/*");
				startActivityForResult(intent, 0);
			}
		});

		save = (Button) findViewById(R.id.save);
		save.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				if (name.getText().length() != 0) {
					AsyncTask<Object, Object, Object> saveContactTask = new AsyncTask<Object, Object, Object>() {
						@Override
						protected Object doInBackground(Object... params) {
							saveContact();
							return null;
						}

						@Override
						protected void onPostExecute(Object result) {
							finish();
						}
					};

					saveContactTask.execute((Object[]) null);
				} else {
					AlertDialog.Builder alert = new AlertDialog.Builder(
							AddEditContacts.this);
					alert.setTitle("Error In Save Contact");
					alert.setMessage("You need to Enter Name of the Contact");
					alert.setPositiveButton("OK", null);
					alert.show();
				}
			}
		});
	}

	private void saveContact() {

		if(yourSelectedImage!=null){
			ByteArrayOutputStream outStr = new ByteArrayOutputStream();
			yourSelectedImage.compress(CompressFormat.JPEG, 100, outStr);
			blob = outStr.toByteArray();
		}
		
		else{blob=image;}

		SQLiteConnector sqlCon = new SQLiteConnector(this);

		if (getIntent().getExtras() == null) {
			sqlCon.insertContact(name.getText().toString(), phone.getText()
					.toString(), mail.getText().toString(), fb.getText()
					.toString(), blob);
		}

		else {
			sqlCon.updateContact(id, name.getText().toString(), phone
					.getText().toString(), mail.getText().toString(), fb
					.getText().toString(), blob);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,Intent resultdata) {
		super.onActivityResult(requestCode, resultCode, resultdata);
		switch (requestCode) {
		case 0:
			if (resultCode == RESULT_OK) {
				Uri selectedImage = resultdata.getData();
				String[] filePathColumn = { MediaStore.Images.Media.DATA };

				Cursor cursor = getContentResolver().query(selectedImage,
						filePathColumn, null, null, null);
				cursor.moveToFirst();

				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
				String filePath = cursor.getString(columnIndex);

				cursor.close();
				// Convert file path into bitmap image using below line.
				yourSelectedImage = BitmapFactory.decodeFile(filePath);
				photo.setImageBitmap(yourSelectedImage);
			}

		}
	}

}
